package com.itheima.service;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.R;
import com.itheima.mapper.OrdersMapper;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Envy
 */
@Service
public class OrdersService {
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    ShoppingCartMapper shoppingCartMapper;

    /**
     * 提交订单
     */
    public void submitOrder(Orders orders) throws Exception {
        //获取时间戳设置id
        long timeMillis = System.currentTimeMillis();
        String orderId = timeMillis + "";
        //设置订单号
        orders.setId(orderId);
        //设置订单编号
        orders.setNumber(orderId);

        //获取用户对象
        User user = (User) redisTemplate.opsForValue().get("token");
        //设置订单用户id
        orders.setUserId(user.getId());
        //设置订单用户手机号
        orders.setPhone(user.getPhone());
        //设置用户名
        String name = user.getName();
        orders.setUserName(name);

        //获取地址对象
        Addressbook ab = ordersMapper.selectAddressBook();
        //设置订单用户名称(地址)
        orders.setConsignee(ab.getConsignee());
        //设置订单地址
        orders.setAddress(ab.getDetail());


        //获取订单详情对象
        OrderDetails orderDetails = new OrderDetails();

        BigDecimal total = new BigDecimal(0);
        List<ShoppingCart> shoppingCart = ordersMapper.selectListShoppingCart();
        if (CollectionUtils.isEmpty(shoppingCart)) {
            throw new Exception("购物车不能为空");
        }

        //遍历购物车获取金额
        for (ShoppingCart cart : shoppingCart) {
            if (cart == null) {
                continue;
            }
            System.out.println("====================" + cart);

            BigDecimal amount = cart.getAmount();
            BigDecimal number = BigDecimal.valueOf(cart.getNumber());
            BigDecimal price = amount.multiply(number);
            total = total.add(price);

            //将购物车数据拷贝到订单详情
            BeanUtils.copyProperties(cart, orderDetails);
            //获取时间戳设置订单编号
            String id = IdUtil.getSnowflakeNextIdStr();
            orderDetails.setId(id);
            orderDetails.setOrdersId(orderId);
            //将数据写进表内
            ordersMapper.insertOrderDetails(orderDetails);
        }
        //设置订单总金额
        orders.setAmount(total);
        //清空购物车
        shoppingCartMapper.cleanshoppingCart();

        ordersMapper.submitOrder(orders);
    }

    /**
     * 订单列表
     */
    public R<OrdersPage> ordersList(Integer page, Integer pageSize) {
        //查询订单表内容放进records集合
        List<OrdersList> ordersLists = ordersMapper.OrdersList(page, pageSize);

        //遍历ordersLists集合得到对象将查询到的订单详情表数据放进去
        for (OrdersList list : ordersLists) {
            String id = list.getId();
            //查询订单详情表放进orderDetails集合中
            List<OrderDetails> orderDetails = ordersMapper.selectOrderDetails(id);
            list.setOrderDetails(orderDetails);
        }

        OrdersPage page1 = new OrdersPage();
        //设置所有页面的总条数
        page1.setTotal(ordersMapper.count());
        //设置每页的显示数据条数
        page1.setPageSize(pageSize);

        //设置显示的当前页面
        page1.setPage(page);
        //把查询到的所有订单数据放进集合
        page1.setRecords(ordersLists);
        return R.success(page1);
    }
}
